home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / SuperMarquee-c / SuperMarquee.c < prev    next >
Encoding:
Text File  |  1994-12-04  |  4.4 KB  |  170 lines  |  [TEXT/MMCC]

  1. //• NewMarquee.c
  2.  
  3. #define NIL 0L
  4.  
  5. BitMap    StringToBitMap (Str255 s, int h, int v, int descent);
  6. void    InitManagers (void);
  7. void MarqueeDisplay (Str255 s, int displayWidth);
  8. void MarqueeIdle (void);
  9. void MarqueeInit (void);
  10.  
  11. typedef struct MarqueeRec {
  12.     struct MarqueeRec    *fNext;
  13.     int            fType;
  14.     Rect        fFromRect, fToRect, newRect;
  15.     BitMap        fBits;
  16.     int            fOffset;
  17.     int            fLeftMaximum;
  18.     long        fDelayTimer;
  19.     WindowPtr    fMarqueePort;
  20. } MarqueeRec, *MarqueePtr;
  21.  
  22. QHdr    gMarqueeQueue;
  23.  
  24. main ()
  25. {
  26.     Rect        r;
  27.     WindowPtr    w;
  28.     EventRecord e;
  29.     Str255        s1 = "\p This string is too long. ", 
  30.                 s2 = "\p This string is also extremely way, way, too long!!! ",
  31.                 s3 = "\p If you can read this you are driving too close for proper traffic operational safety and personal well being! ",
  32.                 s4 = "\p Four score and seven years ago, our forefathers brought forth upon this continent a new nation.  Conceived in liberty............ ",
  33.                 s5 = "\p SUPERMAN!  Strange visitor from another planet, who came to Earth, with powers and abilities far beyond those of mortal men! ";
  34.     
  35.     InitManagers ();
  36.     MarqueeInit ();
  37.     SetRect (&r, 40, 40, 240, 290);
  38.     w = NewWindow (NIL, &r, "\pMarquee Display", true, rDocProc, (WindowPtr)-1L, false, 0);
  39.     SetPort (w);
  40.     TextFont (newYork);
  41.     MoveTo (100, 30);
  42.     MarqueeDisplay (s1, 100);
  43.     TextFont (0);
  44.     MoveTo (100, 60);
  45.     MarqueeDisplay (s2, 100);
  46.     TextFont (monaco);
  47.     TextSize (7);
  48.     MoveTo (100, 85);
  49.     MarqueeDisplay (s3, 100);
  50.     TextFont (courier);
  51.     TextSize (48);
  52.     MoveTo (100, 138);
  53.     MarqueeDisplay (s4, 100);
  54.     TextFont (0);
  55.     TextSize (48);
  56.     MoveTo (100, 218);
  57.     MarqueeDisplay (s5, 100);
  58.     while (! GetNextEvent (mDownMask, &e) )
  59.         MarqueeIdle ();
  60. }
  61.  
  62. void    InitManagers (void)
  63. {
  64.     InitGraf (&qd.thePort);
  65.     InitFonts ();
  66.     InitWindows ();
  67.     InitMenus ();
  68.     TEInit ();
  69.     InitDialogs (NIL);
  70.     FlushEvents (everyEvent, 0);
  71.     InitCursor ();
  72. }
  73.  
  74. /***************************/
  75. BitMap    StringToBitMap (Str255 s, int h, int v, int descent)
  76. {
  77.     GrafPtr        saved;
  78.     GrafPort    newPort;
  79.     Rect        r;
  80.  
  81.     GetPort (&saved);
  82.     OpenPort (&newPort);
  83.     SetRect (&r, 0, 0, h, v);
  84.     BlockMove (&r, &newPort.portRect, sizeof (Rect));
  85.  
  86.     RectRgn (newPort.visRgn, &r);
  87.     RectRgn (newPort.clipRgn, &r);
  88.     newPort.portBits.rowBytes = ((h + 15) / 16) * 2;
  89.     newPort.portBits.baseAddr = NewPtr (v * newPort.portBits.rowBytes );
  90.     BlockMove (&r, &newPort.portBits.bounds, sizeof (Rect));
  91.  
  92.     EraseRect (&r);
  93.     MoveTo (0, v - descent);
  94.     TextFont (saved->txFont);
  95.     TextFace (saved->txFace);
  96.     TextSize (saved->txSize);
  97.     DrawString (s);
  98.  
  99.     SetPort (saved);
  100.     return newPort.portBits;
  101. }
  102. /***************************/
  103. void MarqueeInit ()
  104. {
  105.   gMarqueeQueue.qFlags = 0;
  106.   gMarqueeQueue.qHead = NIL;
  107.   gMarqueeQueue.qTail = NIL;
  108. }  
  109. /***************************/
  110. void MarqueeIdle (void )
  111. {
  112.     MarqueePtr    thisRec;
  113.     
  114.     thisRec = (MarqueePtr) gMarqueeQueue.qHead;
  115.     for (; thisRec != NIL; thisRec = thisRec->fNext )
  116.         if (thisRec->fOffset != 0 && TickCount () > thisRec->fDelayTimer)
  117.         {
  118.             thisRec->fDelayTimer = TickCount ();// + 2;
  119.             
  120.             CopyBits (&thisRec->fBits, &thisRec->fMarqueePort->portBits, &thisRec->fFromRect, &thisRec->fToRect, srcCopy, NIL);
  121.             
  122.             if (thisRec->fFromRect.left + thisRec->fOffset < 0 || thisRec->fFromRect.left + thisRec->fOffset > thisRec->fLeftMaximum) 
  123.             {
  124.                 thisRec->fOffset = -thisRec->fOffset;
  125.                 thisRec->fDelayTimer = TickCount ();// + 40;
  126.             }
  127.             else
  128.                 OffsetRect (&thisRec->fFromRect, thisRec->fOffset, 0);
  129.         }
  130. }
  131. /***************************/
  132. void MarqueeDisplay (Str255 s, int displayWidth)
  133. {
  134.     int            sWidth, dH, lineHeight;
  135.     Point        curLoc;
  136.     FontInfo    fontstuff;
  137.     MarqueePtr    newRec;
  138.     
  139.     sWidth = StringWidth (s);
  140.     if (sWidth < displayWidth) 
  141.     {
  142.         Move (- (sWidth / 2), 0);
  143.         DrawString (s);
  144.      }
  145.     else
  146.         {
  147.             newRec = (MarqueePtr) (NewPtrClear (sizeof (MarqueeRec) + 4) + 4);
  148.             
  149.             GetFontInfo (&fontstuff);
  150.             
  151.             lineHeight = fontstuff.ascent + fontstuff.descent + fontstuff.leading;
  152.             newRec->fBits = StringToBitMap (s, sWidth, lineHeight, fontstuff.descent);
  153.             
  154.             SetRect (&newRec->fFromRect, 0, 0, displayWidth, lineHeight);
  155.             
  156.             BlockMove (&newRec->fFromRect, &newRec->fToRect, sizeof (Rect));
  157.             GetPen (&curLoc);
  158.             OffsetRect (&newRec->fToRect, curLoc.h - displayWidth / 2, curLoc.v - fontstuff.ascent);
  159.             InsetRect (&newRec->fToRect, -2, -2);
  160.             FrameRect (&newRec->fToRect);
  161.             InsetRect (&newRec->fToRect, 2, 2);
  162.             newRec->fLeftMaximum = sWidth - displayWidth;
  163.             newRec->fOffset = 1;
  164.             newRec->fDelayTimer = TickCount ();// +1;
  165.             GetPort (&newRec->fMarqueePort);
  166.             
  167.             Enqueue ((QElemPtr) newRec, &gMarqueeQueue );
  168.      }
  169. }
  170.